package isbnvalidation;

import exceptions.NotAValidISBNException;

/*
 * This file is part of the source of
 * 
 * Probatron4J - a Schematron validator for Java(tm)
 * 
 * Copyright (C) 2010 Griffin Brown Digitial Publishing Ltd
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
// EDIT POR GASTON: cambie los throwns de MalformedISBNException por NotAValidISBNException

/**
 * @author andrews
 * 
 *         Class to represent a *10-digit* ISBN.
 */
public class ISBN10 {
    private String isbn = null;

    /**
     * Constructor.
     */
    public ISBN10(final String isbn) throws NotAValidISBNException {
        this.isbn = isbn;
        if (isbn == null || isbn.length() != 10) {
            throw new NotAValidISBNException("Bad ISBN length");
        }
    }

    public String getIsbn() {
        return this.isbn;
    }

    public void setIsbn(final String isbn) {
        this.isbn = isbn;
    }

    /**
     * @param isbn
     *            string of the ISBN to be parsed
     * @return checksum for this ISBN
     */
    public int getCheckSum() {
        int sum = 0;
        int weight = 0;

        for (int i = 0; i <= 8; i++) {
            switch (i) {
            case 0:
                weight = 10;
                break;
            case 1:
                weight = 9;
                break;
            case 2:
                weight = 8;
                break;
            case 3:
                weight = 7;
                break;
            case 4:
                weight = 6;
                break;
            case 5:
                weight = 5;
                break;
            case 6:
                weight = 4;
                break;
            case 7:
                weight = 3;
                break;
            case 8:
                weight = 2;
                break;
            default:
                weight = 10;
                break;
            }

            char charac = this.isbn.charAt(i);
            int num = Character.digit(charac, 10); // convert character to digit
            sum += num * weight;
        }

        int cSum = sum % 11;

        return cSum == 0 ? cSum : 11 - sum % 11;
    }

    /**
     * @return whether this ISBN has a correct checksum
     */
    public boolean hasCorrectChecksum() {
        char checksumChar;
        int checksum = this.getCheckSum(); // get the calculated check sum digit

        if (checksum == 10) {
            checksumChar = 'X';
        } else {
            checksumChar = Character.forDigit(checksum, 10); // convert digit to character
        }

        char fileDigit = this.isbn.charAt(9); // check sum char

        return checksumChar == fileDigit;
    }

    @Override
    public String toString() {
        return this.isbn;
    }

}